package com.yiqi.liebang.feature.mine.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.DateTool;
import com.yiqi.liebang.common.widget.dialog.ActionSheetDialog;
import com.yiqi.liebang.common.widget.dialog.OnOperItemClickL;
import com.yiqi.liebang.entity.bo.EducationBo;
import com.yiqi.liebang.entity.bo.ImgBean;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.WorkBo;
import com.yiqi.liebang.feature.mine.contract.CertificationContract;
import com.yiqi.liebang.feature.mine.dependencies.certification.CertificationPresenterModule;
import com.yiqi.liebang.feature.mine.dependencies.certification.DaggerCertificationComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.yiqi.liebang.common.util.DateTool.DEFAULT_SDF;

public class UploadWorkActivity extends BaseActivity implements OnOperItemClickL, ActionSheetDialog.MyClickListenerInterface,
        Toolbar.OnMenuItemClickListener, CertificationContract.View {

    @Inject
    CertificationContract.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.view_all)
    LinearLayout mViewAll;
    @BindView(R.id.btn_upload_work_mp)
    ImageView mBtnUploadWorkMp;
    @BindView(R.id.btn_upload_work_zzzm)
    ImageView mBtnUploadWorkZzzm;
    @BindView(R.id.btn_upload_work_yyzz)
    ImageView mBtnUploadWorkYyzz;
    @BindView(R.id.btn_upload_work_gp)
    ImageView mBtnUploadWorkGp;
    @BindView(R.id.view_tip_mp)
    LinearLayout mViewTipMp;
    @BindView(R.id.view_tip_zzzm)
    LinearLayout mViewTipZzzm;
    @BindView(R.id.view_tip_yyzz)
    LinearLayout mViewTipYyzz;
    @BindView(R.id.view_tip_gp)
    LinearLayout mViewTipGp;
    private ActionSheetDialog dialog;
    private List<LocalMedia> selectList = new ArrayList<>();

    private String mMPImageUrl;
    private String mZZZMImageUrl;
    private String mYYZZImageUrl;
    private String mGPImageUrl;
    private String mAuthImage ;
    private int mSelectType = 1;
    private String mWorkId = "";

   WorkBo mImages;

    private boolean isAll = false;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_upload_work;
    }


    @Override
    protected void initInjector() {
        DaggerCertificationComponent.builder().certificationPresenterModule(new CertificationPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        initToolBar(mToolbar, "上传证明材料", true, true);
        mToolbar.setOnMenuItemClickListener(this);
        final String[] stringItems = {"拍照", "从手机相册选择"};
        dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(this);
        dialog.setListener(this);
    }
    @Override
    protected void initData() {
        mWorkId = this.getIntent().getStringExtra("work_id");
        mImages = (WorkBo) this.getIntent().getSerializableExtra("works_imgs");
        isAll = this.getIntent().getBooleanExtra("isAll", false);
        if (mImages != null) {
                if (!TextUtils.isEmpty(mImages.getCertificateImage())) {
                    AM.image().bind(mImages.getLicenseImage(), mBtnUploadWorkZzzm );
                } else if (!TextUtils.isEmpty(mImages.getLicenseImage())) {
                    AM.image().bind(mImages.getLicenseImage(), mBtnUploadWorkYyzz );
                } else if (!TextUtils.isEmpty(mImages.getWorkCardImage())) {
                    AM.image().bind(mImages.getWorkCardImage(),mBtnUploadWorkGp);
                } else if (!TextUtils.isEmpty(mImages.getVisitingImage())) {
                    AM.image().bind(mImages.getVisitingImage(), mBtnUploadWorkMp);
                }

        }
        mToolbar.inflateMenu(isAll ? R.menu.menu_save : R.menu.menu_commit);
    }

    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
        mViewTipMp.setVisibility(View.GONE);
        mViewTipZzzm.setVisibility(View.GONE);
        mViewTipYyzz.setVisibility(View.GONE);
        mViewTipGp.setVisibility(View.GONE);
        switch (position) {
            case 0:
                PictureSelector.create(this)
                        .openCamera(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style1)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(true)
                        .enableCrop(true)// 是否裁剪
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
//                        .withAspectRatio(1, 1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .circleDimmedLayer(false)// 是否圆形裁剪
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(000);//结果回调onActivityResult code
                break;
            case 1:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style1)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(true)
                        .enableCrop(true)// 是否裁剪
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .circleDimmedLayer(false)// 是否圆形裁剪
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(999);//结果回调onActivityResult code
                break;

        }

        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @OnClick({R.id.btn_upload_work_mp, R.id.btn_upload_work_zzzm, R.id.btn_upload_work_yyzz, R.id.btn_upload_work_gp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_upload_work_mp:
                mSelectType = 3;
                mViewTipMp.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_upload_work_zzzm:
                mSelectType = 4;
                mViewTipZzzm.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_upload_work_yyzz:
                mSelectType = 5;
                mViewTipYyzz.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_upload_work_gp:
                mSelectType = 6;
                mViewTipGp.setVisibility(View.VISIBLE);
                break;
        }

        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void doCancle() {
        mViewTipMp.setVisibility(View.GONE);
        mViewTipZzzm.setVisibility(View.GONE);
        mViewTipYyzz.setVisibility(View.GONE);
        mViewTipGp.setVisibility(View.GONE);
        dialog.dismiss();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case 000:
                case 999:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);

                    switch (mSelectType) {
                        case 3:
                            mZZZMImageUrl = null;
                            mYYZZImageUrl = null;
                            mGPImageUrl = null;
                            for (int i = 0; i < selectList.size(); i++) {
                                mMPImageUrl = selectList.get(i).getCutPath();
                            }
                            break;
                        case 4:
                            mMPImageUrl = null;
                            mYYZZImageUrl = null;
                            mGPImageUrl = null;
                            for (int i = 0; i < selectList.size(); i++) {
                                mZZZMImageUrl = selectList.get(i).getCutPath();
                            }
                            break;
                        case 5:
                            mMPImageUrl = null;
                            mZZZMImageUrl = null;
                            mGPImageUrl = null;
                            for (int i = 0; i < selectList.size(); i++) {
                                mYYZZImageUrl = selectList.get(i).getCutPath();

                            }
                            break;
                        case 6:
                            mMPImageUrl = null;
                            mZZZMImageUrl = null;
                            mYYZZImageUrl = null;
                            for (int i = 0; i < selectList.size(); i++) {
                                mGPImageUrl = selectList.get(i).getCutPath();
                                break;
                            }
                    }


                    if (!TextUtils.isEmpty(mMPImageUrl)) {
                        mAuthImage=mMPImageUrl;
                        AM.image().bind(mMPImageUrl, mBtnUploadWorkMp);
                    } else {
                        AM.image().bind(R.drawable.img_mingpiancailiao, mBtnUploadWorkMp);
                    }
                    if (!TextUtils.isEmpty(mZZZMImageUrl)) {
                        mAuthImage=mZZZMImageUrl;
                        AM.image().bind(mZZZMImageUrl, mBtnUploadWorkZzzm);
                    } else {
                        AM.image().bind(R.drawable.img_zaizhizzhengming, mBtnUploadWorkZzzm);
                    }

                    if (!TextUtils.isEmpty(mYYZZImageUrl)) {
                        mAuthImage=mYYZZImageUrl;
                        AM.image().bind(mYYZZImageUrl, mBtnUploadWorkYyzz);
                    } else {
                        AM.image().bind(R.drawable.img_yingyezhizhao, mBtnUploadWorkYyzz);
                    }

                    if (!TextUtils.isEmpty(mGPImageUrl)) {
                        mAuthImage=mGPImageUrl;
                        AM.image().bind(mGPImageUrl, mBtnUploadWorkGp);
                    } else {
                        AM.image().bind(R.drawable.img_gongpai, mBtnUploadWorkGp);
                    }

                    break;
            }
        }

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        if (isAll) {
           mPresenter.uploadAuthenticationImage(mWorkId,mSelectType+"",mAuthImage);
        }else {
            mPresenter.authWork(mWorkId, mZZZMImageUrl, mYYZZImageUrl, mGPImageUrl, mMPImageUrl);
        }


        return false;
    }



    @Override
    public void showWorkSuccess(WorkBo workBo) {

    }

    @Override
    public void showUserCardSuccess(UserCardBo s) {

    }

    @Override
    public void showEducationSuccess(EducationBo workBo) {

    }

    @Override
    public void showAuthWorkSuccess() {
        if (isAll) {
            finish();
        } else {
            Intent intents = new Intent(this, CommitSuccessActivity.class);
            intents.putExtra("date", DateTool.getCurTimeString(DEFAULT_SDF));
            startActivity(intents);
            finish();
        }

    }

    @Override
    public void showCertificationSuccess() {

    }

    @Override
    public void showError(String error) {
        showMsg(error);
    }

    @Override
    public void showWorkListSuccess(List<WorkBo> workBos) {

    }

    @Override
    public void showEduListSuccess(List<EducationBo> workBos) {

    }

    @Override
    public void showUploadAuthenticationImageSuccess() {
        showMsg("保存成功！");
        finish();
    }
}
